layui.use(['jquery', 'element', 'table', 'layer', 'form'], function(){
    var element = layui.element;
    var $ = layui.$;
    var form = layui.form;
    var userTable = layui.table;
    var ctxPath = "/education";

    userTable.render({
        id: 'userTableId',
        elem: '#userTable',
        url: ctxPath + '/user/tableData',
        page: true,
        method: 'post',
        cols:[[
            {checkbox: true},
            {field:'id', title:'ID', width: 150},
            {field:'username', title:'用户名', width: 200},
            {field:'code', title:'code', width: 200 },
            {field:'phone', title:'电话', width: 150},
            {field:'createTime', title:'创建时间', width: 150},
            {fixed: 'right', title:'操作', width:350, align:'center', toolbar: '#userOptBar'}
        ]],
        done: function(res, curr, count){
            //如果是异步请求数据方式，res即为你接口返回的信息。
            //如果是直接赋值的方式，res即为：{data: [], count: 99} data为当前页数据、count为数据总长度
            console.log(res);

            //得到当前页码
            console.log(curr);

            //得到数据总量
            console.log(count);
        }
    });

    var initForm = function(data) {
        $('#userForm').find('input[name="id"]').val(data.id);
        $('#userForm').find('input[name="username"]').val(data.username);
        $('#userForm').find('input[name="code"]').val(data.code);
        $('#userForm').find('input[name="phone"]').val(data.phone);
    };

    var active = {
        addUser: function(){
            layer.open({
                type: 1,
                title: '新增用户',
                maxWidth: 450,
                content: $('#userForm')
            });
            initForm({
                id: '',
                username: '',
                code: '',
                phone: ''
            });
        },
        searchUser: function(){
            var username = $('#username').val();
            if(!!username) {
                username = '%' + $('#username').val() + '%';
            }
            userTable.reload('userTableId', {
                where: {
                    username: username
                }
            });
        }
    };

    //按钮事件
    $('.layui-body .layui-btn').on('click', function(){
        var type = $(this).data('type');
        active[type] ? active[type].call(this) : '';
    });

    //监听工具条
    userTable.on('tool(userTableTool)', function(obj){
        var data = obj.data;
        if(obj.event === 'assignRole'){
            layer.open({
                type: 1,
                title: '分配角色',
                area: ['350px', '300px'],
                content: $('#assignRole')
            });
            $('#assignUserId').val(data.id);
            $('#assignRole').find('input[name="username"]').val(data.username);
             form.render();
        } else if (obj.event === "editUser") {
            layer.open({
                type: 1,
                title: '修改用户',
                maxWidth: 450,
                content: $('#userForm')
            });
            initForm(data);
        } else if (obj.event === "deleteUser") {
            layer.confirm('确定要删除记录?', {icon: 3, title:'确认'}, function(index){
                $.ajax({
                    url: ctxPath + '/user/delete',
                    data: {ids : data.id},
                    type: 'post',
                    dataType: 'json',
                    success: function (result) {
                            if(result.code == '0') {
                                layer.alert("删除成功！");
                                window.location.reload();
                            } else {
                                layer.alert("删除失败：" + result.msg);
                            }
                        }
                    });

                layer.close(index);
            });
         }
    });

    //表单提交
    form.on('submit(saveBtn)', function(data){
        $.ajax({
            url: ctxPath + '/user/saveOrUpdate',
            data: data.field,
            type: 'post',
            dataType: 'json',
            success: function (result) {
                if(result.code == '0') {
                    layer.alert("保存成功！");
                    window.location.reload();
                } else {
                    layer.alert("保存失败：" + result.msg);
                }
            }
        });
        return false;
    });
    //分配角色
    form.on('submit(assignBtn)', function(data){
    console.info(data.field);
        $.ajax({
            url: ctxPath + '/user/saveOrUpdateRole',
            data: data.field,
            type: 'post',
            dataType: 'json',
            success: function (result) {
                if(result.code == '0') {
                    layer.alert("保存成功！");
                    window.location.reload();
                } else {
                    layer.alert("保存失败：" + result.msg);
                }
            }
        });
        return false;
    });
});
